package cn.iocoder.yudao.module.system.dal.mysql.investment;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.investment.SystemParticipation;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author 梁富贵
 * @Date 2024/10/11
 **/
@Mapper
public interface ParticipationMapper extends BaseMapperX<SystemParticipation> {
    default int updateParticipation(SystemParticipation systemParticipation){
        LambdaUpdateWrapper<SystemParticipation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SystemParticipation::getId,systemParticipation.getId());
        wrapper.set(systemParticipation.getParticipationId() !=null,SystemParticipation::getParticipationId,systemParticipation.getParticipationId());
        wrapper.set(systemParticipation.getCompanyName() !=null,SystemParticipation::getCompanyName,systemParticipation.getCompanyName());
        wrapper.set(systemParticipation.getLegalRepresentative() !=null,SystemParticipation::getLegalRepresentative,systemParticipation.getLegalRepresentative());
        wrapper.set(systemParticipation.getPhone() !=null,SystemParticipation::getPhone,systemParticipation.getPhone());
        wrapper.set(systemParticipation.getAddress() !=null,SystemParticipation::getAddress,systemParticipation.getAddress());
        wrapper.set(systemParticipation.getProjectId() !=null,SystemParticipation::getProjectId,systemParticipation.getProjectId());
        wrapper.set(systemParticipation.getStatus() !=null,SystemParticipation::getStatus,systemParticipation.getStatus());
        wrapper.set(systemParticipation.getApplicationDate() !=null,SystemParticipation::getApplicationDate,systemParticipation.getApplicationDate());
        wrapper.set(systemParticipation.getApprovalDate() !=null,SystemParticipation::getApprovalDate,systemParticipation.getApprovalDate());
        wrapper.set(SystemParticipation::getNotes,systemParticipation.getNotes());
        wrapper.set(systemParticipation.getApplicationFile()!=null,SystemParticipation::getApplicationFile,systemParticipation.getApplicationFile());
        wrapper.set(systemParticipation.getQualicationFile()!=null,SystemParticipation::getQualicationFile,systemParticipation.getQualicationFile());
        wrapper.set(systemParticipation.getInvestmentId() !=null,SystemParticipation::getInvestmentId,systemParticipation.getInvestmentId());
        return update(systemParticipation,wrapper);
    }

    default List<SystemParticipation> selectParticipationListByUserId(ParticipationReqVO reqVO){
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        LambdaQueryWrapperX<SystemParticipation> query = new LambdaQueryWrapperX<>();
        query.likeIfPresent(SystemParticipation::getCompanyName, reqVO.getCompanyName())
                .eqIfPresent(SystemParticipation::getParticipationId, reqVO.getParticipationId())
                .eqIfPresent(SystemParticipation::getId, reqVO.getId())
                .between(reqVO.getApplicationDate().length==2,SystemParticipation::getApplicationDate, reqVO.getApplicationDate()[0], reqVO.getApplicationDate()[1]);
        return selectList(query);
    }

    /**
     * 根据用户id查询参与的项目id
     * @param userId
     * @return
     */
    default List<Integer> selectProjectIds(Long userId){
        LambdaQueryWrapperX<SystemParticipation> query = new LambdaQueryWrapperX<>();
        query.select(SystemParticipation::getProjectId);
        return selectList(query).stream().map(SystemParticipation::getProjectId).collect(Collectors.toList());
    }

    default List<Long> selectParticipationIdsByProjectIds(ParticipationReqVO reqVO,List<Integer> projectIds){
        if(Objects.isNull(projectIds) || projectIds.isEmpty()){
            return null;
        }
        LambdaQueryWrapperX<SystemParticipation> query = new LambdaQueryWrapperX<>();
        query.likeIfPresent(SystemParticipation::getCompanyName, reqVO.getCompanyName());
        if(!Objects.isNull(reqVO.getApplicationDate()) && reqVO.getApplicationDate().length==2){
            query.betweenIfPresent(SystemParticipation::getApplicationDate, reqVO.getApplicationDate()[0], reqVO.getApplicationDate()[1]);

        }
        query.in(SystemParticipation::getProjectId,projectIds);
        query.select(SystemParticipation::getParticipationId);
        return selectList(query).stream().map(SystemParticipation::getParticipationId).distinct().collect(Collectors.toList());
    }

    default Integer passingParticipationApplication(List<Integer> participationIds){

        LambdaUpdateWrapper<SystemParticipation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SystemParticipation::getId,participationIds);
        wrapper.eq(WebFrameworkUtils.getLoginUserId()!=null,SystemParticipation::getInvestmentId, WebFrameworkUtils.getLoginUserId());
        wrapper.set(SystemParticipation::getStatus,1);
        return update(wrapper);
    }

    default Integer rejectedParticipationApplication(List<Integer> participationIds){

        LambdaUpdateWrapper<SystemParticipation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SystemParticipation::getId,participationIds);
        wrapper.eq(WebFrameworkUtils.getLoginUserId()!=null,SystemParticipation::getInvestmentId, WebFrameworkUtils.getLoginUserId());
        wrapper.set(SystemParticipation::getStatus,2);
        return update(wrapper);
    }
}
